package com.sumainfor.zuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.sumainfor.common.Constants;
import com.sumainfor.zuul.util.HttpContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class WxZullFilter extends ZuulFilter {

    private Logger log = LoggerFactory.getLogger(getClass());

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 1;
    }

    @Override
    public boolean shouldFilter() {
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String path = request.getServletPath();
        if (path.startsWith("/sys/")) {
            return false;
        } else if (path.startsWith("/wechat/")) {
            return true;
        }
        return false;
    }

    @Override
    public Object run() throws ZuulException {
        //判断权限
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();

        RequestContext ctx = RequestContext.getCurrentContext();

        ctx.addZuulRequestHeader(Constants.HEAD_TYPE, String.valueOf(Constants.HeadType.WX.getCode()));

        log.info("转发服务URL ===> {}\n转发附带Header ====> {} \n 参数 ====> {}",
                request.getServletPath(),
                ctx.getZuulRequestHeaders().toString(),
                request.getParameterMap().toString());

        return null;
    }
}
